// 引入 react 依赖
import React, { Component } from 'react';
import { Provider, connect } from 'react-redux';
import { StyleSheet, TextInput, Text, View } from 'react-native';
import { Router, Scene, Modal, Tabs } from 'react-native-router-flux';

// 引入 redux 所需文件
import store from './src/store/store.js'
import * as ActionTypes from './src/constants/constants.js'

// import SimpleApp from './src/router/router.js'

// import { AppId } from './src/util/utils.js'
import *  as AntDesign from '@ant-design/react-native'

// 引入第三方依赖
// import _ from 'lodash';
import * as WeChat from 'react-native-wechat';
import pureRender from 'pure-render-decorator';
import SplashScreen from 'react-native-splash-screen'
import { CacheHelper } from "react-native-rn-cacheimage";
import { Actions } from 'react-native-router-flux';

// 引入组件
import {
    Home,
    Category,
    NewProduct,
    ShoppingCart,
    User,
    CategoryDetails,
    GoodDetails,
    CodeLogin,
    Login,
    Registered,
    ResetPassword,
    SubmitOrders,
    SubmitBook,
    SubmitMagazine,
    Magazine,
    MagazineDetails,
    Address,
    EditAddress,
    Spot,
    Purchasing,
    UserInfo,
    OrderList,
    Search,
    GoodsList,
    OrderInfo,
    Headset,

    LoginState,
    QrCode,
    Pay,
    PayResults,
    PayAtm
} from './src/router/index.js'; //引入文件

import { getUserInfo } from './src/router/User/module/module.js'
import { getDefaultAddressList } from './src/router/Address/module/module'


import { TabBar } from './src/components/index.js'; //引入文件
// [RNSplashScreen show];

import { AppId, isUser, isWxUser, loadData, isLogin, logout, removeData } from './src/util/utils.js'

// 修改defaultProps 设置allowFontScaling = false
TextInput.defaultProps = Object.assign({}, TextInput.defaultProps, { defaultProps: false })
Text.defaultProps = Object.assign({}, Text.defaultProps, { allowFontScaling: false })

class App extends Component {

    constructor(props) {
        super(props);
        this.state = {
            appid: AppId,
        };
        WeChat.registerApp(AppId)
        this.getUserInfo = getUserInfo.bind(this)
    }
    componentDidMount() {
        SplashScreen.hide()
        CacheHelper.register({ overwrite: false }).catch(e => console.log(e))
        loadData('authorization', (auth) => {
            if (auth) {
                store.dispatch({
                    type: ActionTypes.auth,
                    data: auth
                })
                loadData('userInfo', (userInfo) => {
                    if (userInfo) {
                        getDefaultAddressList()
                        store.dispatch({
                            type: ActionTypes.userInfo,
                            data: userInfo
                        })
                    }
                })
            }
        })
    }

    componentWillUnmount() {
        CacheHelper.unregister().catch(e => console.log(e))
    }

    checkLogin = () => {
        if (!isLogin()) {
            logout(true)
            return false
        }
    }

    exitLogin = () => {

        // logout(false)
        // Actions.replace('home')
        // return false
    }

    render() {
        const onBackPress = () => {
            if (Actions.state.index !== 0) {
                return false
            }
            Actions.pop()
            return true
        }
        const hideBack = { hideNavBar: true, hideBackImage: true, onBack: () => { } }
        const normalNav = { navigationBarStyle: { backgroundColor: '#EB4B6E', borderBottomWidth: 0, elevation: 0 }, titleStyle: { color: '#fff' } }
        return (
            <Provider store={store}>
                {/* <SimpleApp /> */}
                <AntDesign.Provider>
                    <Router backAndroidHandler={onBackPress}>
                        <Modal hideNavBar={true}>
                            <Scene key="root" hideNavBar={true}>
                                <Scene key="index" initial={true} hideNavBar={true} sceneKey="index"  {...normalNav}>
                                    <Tabs
                                        key="tabBar"        // 唯一标识
                                        wrap={true}         // 自动使用自己的导航栏包装每个场景
                                        showLabel={false}   // 显示文字
                                        tabBarStyle={styles.tabbarContaine} // tabBar的样式
                                        swipeEnabled={false}// 是否可以滑动
                                        headerMode='screen' // 页面切换方式
                                        icon={TabBar}      // 自定义Icon显示方式
                                        lazy={true}         // 是否默认渲染tabbar
                                        tabBarPosition={'bottom'}       // tabbar在顶部还是底部，iOS默认顶部，安卓默认顶部
                                    >
                                        <Scene key="home" title="首页" component={Home} hideNavBar={true} sceneKey="home"  {...normalNav} />
                                        <Scene key="category" title="分类" component={Category} hideNavBar={true} sceneKey="category"  {...normalNav} />
                                        {/* <Scene key="newProduct" title="新品" component={NewProduct} hideNavBar={true} sceneKey="newProduct"  {...normalNav} /> */}
                                        <Scene key="shoppingCart" title="购物车" component={ShoppingCart} hideNavBar={true} sceneKey="shoppingCart"  {...normalNav} />
                                        <Scene key="user" title="我的" component={User} hideNavBar={true} sceneKey="user"  {...normalNav} />
                                        <Scene key="customer" title="联系客服" component={Headset} hideNavBar={true} sceneKey="customer"  {...normalNav} />
                                    </Tabs>
                                </Scene>
                                <Scene key="spot" title="现货" component={Spot} hideNavBar={true} sceneKey="spot"  {...normalNav} />
                                <Scene key="purchasing" title="代购" component={Purchasing} hideNavBar={true} sceneKey="purchasing"  {...normalNav} />
                                <Scene key="categoryDetails" title="分类详情" component={CategoryDetails} hideNavBar={true} sceneKey="categoryDetails"  {...normalNav} />
                                <Scene key="goodDetails" title="商品详情" component={GoodDetails} hideNavBar={true} sceneKey="goodDetails"  {...normalNav} />
                                <Scene key="codeLogin" title="验证码登录" component={CodeLogin} hideNavBar={true} sceneKey="codeLogin"  {...normalNav} />
                                <Scene key="resetPassword" title="重置密码" component={ResetPassword} hideNavBar={true} sceneKey="resetPassword"  {...normalNav} />
                                <Scene key="address" title="选择地址" onEnter={this.checkLogin} component={Address} hideNavBar={true} sceneKey="address"  {...normalNav} />
                                <Scene key="editAddress" title="新增修改地址" onEnter={this.checkLogin} component={EditAddress} hideNavBar={true} sceneKey="editAddress"  {...normalNav} />
                                <Scene key="submitOrders" title="提交订单" onEnter={this.checkLogin} component={SubmitOrders} hideNavBar={true} sceneKey="submitOrders"  {...normalNav} />
                                <Scene key="submitBook" title="提交书籍订单" onEnter={this.checkLogin} component={SubmitBook} hideNavBar={true} sceneKey="submitBook"  {...normalNav} />
                                <Scene key="submitMagazine" title="提交杂志订购订单" onEnter={this.checkLogin} component={SubmitMagazine} hideNavBar={true} sceneKey="submitMagazine"  {...normalNav} />
                                <Scene key="magazine" title="杂志订购" component={Magazine} hideNavBar={true} sceneKey="magazine"  {...normalNav} />
                                <Scene key="magazineDetails" title="杂志详情" component={MagazineDetails} hideNavBar={true} sceneKey="magazineDetails"  {...normalNav} />
                                <Scene key="userInfo" title="账户信息" onEnter={this.checkLogin} component={UserInfo} hideNavBar={true} sceneKey="userInfo"  {...normalNav} />
                                <Scene key="orderList" title="订单列表" onEnter={this.checkLogin} component={OrderList} hideNavBar={true} sceneKey="orderList"  {...normalNav} />
                                <Scene key="search" title="搜索" component={Search} hideNavBar={true} sceneKey="search"  {...normalNav} />
                                <Scene key="goodsList" title="搜索结果" component={GoodsList} hideNavBar={true} sceneKey="goodsList"  {...normalNav} />
                                <Scene key="orderInfo" title="订单详情" onEnter={this.checkLogin} component={OrderInfo} hideNavBar={true} sceneKey="orderInfo"  {...normalNav} />
                                <Scene key="headset" title="联系客服" component={Headset} hideNavBar={true} sceneKey="headset"  {...normalNav} />
                                <Scene key="loginState" title="登录" component={LoginState} hideNavBar={true} sceneKey="loginState"  {...normalNav} />
                                <Scene key="qrCode" title="扫码" component={QrCode} hideNavBar={true} sceneKey="qrCode"  {...normalNav} />
                                <Scene key="pay" modal={true} title="支付" component={Pay} hideNavBar={true} sceneKey="pay"  {...normalNav} />
                                <Scene key="payResults" modal={true} title="支付结果" component={PayResults} hideNavBar={true} sceneKey="payResults"  {...normalNav} />
                                <Scene key="payAtm" modal={true} title="支付结果" component={PayAtm} hideNavBar={true} sceneKey="payAtm"  {...normalNav} />
                                <Scene key="login" title="登录" component={Login} hideNavBar={true} sceneKey="login"  {...normalNav} />
                                <Scene key="registered" title="注册" component={Registered} hideNavBar={true} sceneKey="registered"  {...normalNav} />
                            </Scene>
                        </Modal>
                    </Router>
                </AntDesign.Provider>
            </Provider>
        )
    }
}

const styles = StyleSheet.create({
    tabbarContainer: {

    },
})

export default App;
